home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / perlcl16.lha / perlclass1.6 / regexp.h < prev    next >
C/C++ Source or Header  |  1992-11-09  |  2KB  |  125 lines

  1. /*
  2.  * version 1.6
  3.  * Regexp is a class that encapsulates the Regular expression
  4.  * stuff. Hopefully this means I can plug in different regexp
  5.  * libraries without the rest of my code needing to be changed.
  6.  * Written by Jim Morris,  jegm@sgi.com
  7.  */
  8. #ifndef    _REGEXP_H
  9. #define _REGEXP_H
  10. #include    <iostream.h>
  11. #include    <stdlib.h>
  12. #include    <malloc.h>
  13. #include    <string.h>
  14. #include    <assert.h>
  15. #include    <ctype.h>
  16.  
  17. #include    "regex.h"
  18.  
  19. /*
  20.  * Note this is an inclusive range where it goes
  21.  * from start() to, and including, end()
  22.  */
  23. class Range
  24. {
  25. private:
  26.     int st, en;
  27.     
  28. public:
  29.     Range()
  30.     {
  31.     st=0; en= -1;
  32.     }
  33.     
  34.     Range(int s, int e)
  35.     {
  36.     st= s; en= e;
  37.     }
  38.     
  39.     int start(void) const { return st;}
  40.     int end(void) const { return en;}
  41.     int length(void) const { return (en-st)+1;}
  42. };
  43.  
  44. class Regexp
  45. {
  46. public:
  47.     enum options {def=0, nocase=1};
  48.     
  49. private:
  50.     regexp *repat;
  51.     const char *target; // only used as a base address to get an offset
  52.     int res;
  53.     int iflg;
  54. #ifndef    __TURBOC__
  55.     void strlwr(char *s)
  56.     {
  57.     while(*s){
  58.         *s= tolower(*s);
  59.         s++;
  60.     }
  61.     }
  62. #endif    
  63. public:
  64.     Regexp(const char *rege, int ifl= 0)
  65.     {
  66.         iflg= ifl;
  67.         if(iflg == nocase){ // lowercase fold
  68.             char *r= new char[strlen(rege)+1];
  69.             strcpy(r, rege);
  70.             strlwr(r);
  71.             if((repat=regcomp(r)) == NULL){
  72.             cerr << "regcomp() error" << endl;
  73.             exit(1);
  74.             }
  75.             delete [] r;
  76.     }else{
  77.         if((repat=regcomp (rege)) == NULL){
  78.             cerr << "regcomp() error" << endl;
  79.             exit(1);
  80.         }
  81.         }
  82.     }
  83.     
  84.     ~Regexp()
  85.     {
  86.     free(repat);
  87.     }    
  88.  
  89.     int match(const char *targ)
  90.     {
  91.         int res;
  92.         if(iflg == nocase){ // fold lowercase
  93.             char *r= new char[strlen(targ)+1];
  94.             strcpy(r, targ);
  95.             strlwr(r);
  96.             res= regexec(repat, r); 
  97.             target= r; // looks bad but is really ok, really
  98.             delete [] r;
  99.         }else{
  100.         res= regexec(repat, targ);
  101.         target= targ;
  102.     }
  103.  
  104.     return ((res == 0) ? 0 : 1);
  105.     }
  106.     
  107.     int groups(void) const
  108.     {
  109.     int res= 0;
  110.     for (int i=0; i<NSUBEXP; i++) {
  111.         if(repat->startp[i] == NULL) break;
  112.         res++;
  113.     }
  114.     return res;
  115.     }
  116.     
  117.     Range getgroup(int n) const
  118.     {
  119.     assert(n < NSUBEXP);
  120.     return Range((int)(repat->startp[n] - (char *)target),
  121.              (int)(repat->endp[n] - (char *)target) - 1);
  122.     }
  123. };
  124. #endif
  125.